<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\common\model\Banner;
use app\common\model\Major;
use app\common\model\Paper;
use app\common\model\Record;

class Course
{
    /**
     * 课程推荐
     *
     * @return \think\Response
     */
    public function index()
    {
        return sucessMsg(200, '获取成功', [
            'banner' => $this->banner(),   //获取课程推荐--banner
            'course' => $this->course(),   //获取课程推荐--课程内容
        ]);
    }

    /**
     * 首页轮播图
     *
     * @return \think\Response
     */
    public function banner()
    {
        $type = input('type/d', 3);
        $data = Banner::where(['type' => $type, 'status' => 1])
            ->field('image')
            ->withAttr('image', function ($val, $data) {
                return strtolower(config("app.domain_url") . str_replace('\\', '/', $val));
            })
            ->order(['id' => 'asc'])
            ->select();
        return $data;
    }

    /**
     * 课程推荐
     *
     * @return \think\Response
     */
    public function course()
    {
        $data1 = Major::where(['status' => 1])
            ->field('id,grade_id,name')
            ->select();

        $data2 = Record::where(['status' => 1])
            ->field('id,paper_id,star_time')
            ->select();

        foreach ($data1 as &$value1) {
            $value1['grade_id'] = $value1->grade->name;
            unset($value1->grade);
        }

        foreach ($data2 as &$value2) {
            $value2['paper_id'] = $value2->paper->title;
            unset($value2->paper);
            $value2['star_time'] = date('Y-m-d H:i:s', $value2['star_time']);
        }
        return $data=['kecheng'=>$data1, 'shijvan'=>$data2];
    }
}

